package cn.richard.demo.tax.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.richard.demo.tax.config.KieUtil;
import cn.richard.demo.tax.dto.Tax;
import cn.richard.demo.tax.entity.DroolsRule;
import cn.richard.demo.tax.service.DroolsRuleService;

@Controller
public class IndexCtrller {
    @Autowired
    DroolsRuleService droolsRuleService;

    @GetMapping({"/", "/index"})
    public String index() {
        return "index";
    }
    
    @GetMapping("/droolsRule/list")
    public String droolsRule_list() {
        return "droolsRule_list";
    }
    @RequestMapping("/droolsRule/page")
    @ResponseBody
    public BaseResult droolsRule_page(Integer page, Integer limit) {
        return BaseResult.succData(droolsRuleService.page(new Page<>(page, limit)));
    }
    @RequestMapping("/droolsRule/form")
    public String droolsRule_form(DroolsRule entity, Model model) {
        if (null != entity.getId()) {
        	entity = droolsRuleService.getById(entity.getId());
            model.addAttribute("droolsRule", entity);
        } else {
            model.addAttribute("droolsRule", new DroolsRule());
        }
        return "droolsRule_form";
    }
    @PostMapping("/droolsRule/post")
    public String post(DroolsRule entity) {
        droolsRuleService.saveOrUpdate(entity);
        //重载规则
        KieUtil.reload();
        return "redirect:/droolsRule/list";
    }
    @RequestMapping("/droolsRule/remove")
    public String remove(@RequestParam Integer id) {
        droolsRuleService.removeById(id);
        //重载规则
        KieUtil.reload();
        return "redirect:/droolsRule/list";
    }
    
    @RequestMapping("/tax/form")
    public String tax_form(@RequestParam(required = false,defaultValue = "0") Double wage, Model model) {
    	Tax tax = new Tax().setWage(wage);
    	KieUtil.fire(tax);
		model.addAttribute("tax", tax);
        return "tax-form";
    }
    
    @RequestMapping("/tax/form/json")
    @ResponseBody
    public Object tax_form(@RequestParam double wage) {
    	Tax tax = new Tax().setWage(wage);
		KieUtil.fire(tax);
        return tax;
    }
    
}
